package main

import (
	"fmt"
	"goweb/car_video"
	"goweb/mqtt"
	"goweb/route"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

//moblie 为移动设备分支名称
func main() {
	r := gin.Default()
	route.Init_route(r)
	r.GET("/login", route.Login)//
	r.GET("/register", route.Register)//
	r.POST("/login", route.Login_POST)
	r.POST("/register", route.Register_POST)
	r.GET("/Invite_code",route.CheckJWT,route.Get_Invite_code)

	r.GET("/win_st_q", route.CheckJWT, route.Win_st_q)// 
	r.GET("/homepage", route.CheckJWT, route.Homepage)// 
	r.GET("/msg_center", route.CheckJWT, route.Msg_center)// 
	r.GET("/ai_chat_room", route.CheckJWT, route.Ai_chat_room)// 
	r.GET("/lost_and_found", route.CheckJWT, route.Lost_and_found)// 
	r.GET("/class_info",route.CheckJWT,route.Get_class)
	r.POST("/ai_chat", route.CheckJWT, route.Ai_chat)// 
	r.POST("/send_msg", route.CheckJWT, route.Recieve_msg)
	r.POST("/text_to_speech", route.CheckJWT, route.Text_to_speech)


	r.GET("/query_msg", route.CheckJWT, route.Query_msg)
	r.GET("/ws_receive_msg", route.CheckJWT,func(c *gin.Context){
		route.Up_ws(c.Writer,c.Request)
	})
	r.GET("/audio", route.CheckJWT, route.Audio)// 
	r.GET("/time", func(ctx *gin.Context) {
		ctx.String(200,time.Now().Format("20060102150405.00000"))
	})
	r.POST("/pic_upload",route.Pic_upload)
	r.GET("/picture",route.CheckJWT,route.Pic_get)
	r.GET("/lost_found_pic",route.CheckJWT,route.Lost_found_pic_get)
	r.GET("/car_video",route.CheckJWT,route.Car_video)
	r.POST("Lost_found_pic_upload",route.CheckJWT,route.Lost_found_pic_upload)
	r.POST("/library_update",route.Library_update)
	r.GET("/library_get",route.Library_get)
	// r.GET("/audio_ws",route.CheckJWT,route.Audio_Websocket_handler)
	r.POST("/car_video_upload",car_video.Car_video_upload)
	r.GET("/car_video_ws",car_video.Car_video_get)
	r.GET("/ip_post",route.Ip_post)
	r.GET("/audio_ws", route.Create_websocket_handler)
	r.GET("/js",route.CheckJWT,func(ctx *gin.Context) {
		str:=ctx.Query("json")
		_,err:=http.Get("http://"+"192.168.1.107"+"/js?json="+str)
		if err!=nil{
			ctx.String(200,"error")
			return
		}
		ctx.String(200,"success")
	})
	go mqtt.Tsos()
	go route.Msg_sender()
	go car_video.Car_video_sender()
	
	go route.ReceiveUDPData(route.UDP_PORT_LISTEN)
	
	go route.Send_audio_data_to_websocket()
	go route.Send_audio_data_to_udp()

	err := r.RunTLS(":12345", "./ssl/server.crt", "./ssl/server.key")
	if err != nil {
		fmt.Println("RunTLS failed:", err)
	}
	// r.Run(":12345")

}
 
